/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ni;

import controller.ChatController;
import controller.User;
import protocol.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;


/**
 *
 * @author CarlaSauvanaud
 */
public class ChatNI {

    private ChatController controller;
    private UDPReceive udps;

    public ChatNI (ChatController controller) {
        this.controller = controller;
        udps = new UDPReceive(controller);
        //Lancement du thread de reception
        udps.start();
        System.out.println("Le thread server run.");
    }

        public UDPReceive getUdps() {
        return udps;
    }

    public void sendHelloBroadcast(boolean b) {
        Hello h = new Hello(b,controller.getLocalUser().getUserName(),controller.getLocalUser().getUserAddress());
  
        UDPSend.envoiMessageBroadcast(h);
    }
    public void sendHelloBack(boolean b,User u) {
        Hello h = new Hello(b,controller.getLocalUser().getUserName(),controller.getLocalUser().getUserAddress());

        UDPSend.envoiMessageUnicast(h,u.getUserAddress());
    }

    public void sendBye() {
        Bye bye = new Bye(controller.getLocalUser().getUserName(),controller.getLocalUser().getUserAddress());
        UDPSend.envoiMessageBroadcast(bye);
    }

    public void sendChatMessage(User u,String s) {
        ChatMessage cm = new ChatMessage(controller.getLocalUser().getUserName(), s, controller.getLocalUser().getUserAddress());
        //u est l'ulisateur distant
        UDPSend.envoiMessageUnicast(cm, u.getUserAddress());
    }

    public void sendFileNotify(User u, File f)
    {
        int dot = f.getName().lastIndexOf(".");
        String ext = f.getName().substring(dot + 1);

        FileNotify fn = new FileNotify(controller.getLocalUser().getUserName(), controller.getLocalUser().getUserAddress(),
                                        f.getName(), ext, f.length());

        UDPSend.envoiMessageUnicast(fn, u.getUserAddress());
    }

    public void sendAckFile(User u, boolean ack)
    {
        AckFile af = new AckFile(controller.getLocalUser().getUserName(), controller.getLocalUser().getUserAddress(), ack);

        UDPSend.envoiMessageUnicast(af, u.getUserAddress());
    }
    
    public void sendFile(File f, InetAddress serverAddress, ChatController c)
    {
        TCPClient client = new TCPClient(serverAddress, f, c);
    }
    
    public void receiveFile(String filename, long taille, ChatController c)
    {
        TCPServer server = new TCPServer(filename, taille, c);
    }
    /**
     * Permet de Sérialiser un objet de type Message
     * @return un tableau de byte
     */
    public static byte[] serialiser(Message m){
             byte[] result=null;

             try {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
                    ObjectOutputStream oos= new ObjectOutputStream(bos);
                    // serialisation : ecriture de l'objet dans le flux de sortie
                    oos.writeObject(m);
                    // on vide le tampon
                    oos.flush();
                    System.out.println(m.toString() + " a ete serialise");
                    result = bos.toByteArray();
             }
             catch(IOException ioe) {
                    ioe.printStackTrace();
                    System.out.println("PROBLEME methode write:" +m + "n'a pas ete serialise");
             }
            return result;
    }

    /**
     *
     * @param messToRead le message a deserializer
     * @return
     * @return un objet de type Message
     */
    public static Message deserialiser(byte[] messToRead){
            Message result = null;

            try {
                    ByteArrayInputStream bis = new ByteArrayInputStream(messToRead);
                    ObjectInputStream ois = new ObjectInputStream(bis);
                    result = (Message) ois.readObject();

            } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("Objet non deserialise");
            } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    System.out.println("Objet non deserialise");
            }

            return result;

    }


}
